// pages/collectSuc/collectSuc.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: "",
    protocolMaskShow: false,
    ischecked: false,
    city: "",
    orderDate: "",
    posterList: ["https://imgtihe.oss-cn-hangzhou.aliyuncs.com/%E8%B6%A3%E4%B8%8A%E5%B1%8F/banner1.png", "https://imgtihe.oss-cn-hangzhou.aliyuncs.com/%E8%B6%A3%E4%B8%8A%E5%B1%8F/banner1.png","https://imgtihe.oss-cn-hangzhou.aliyuncs.com/%E8%B6%A3%E4%B8%8A%E5%B1%8F/banner1.png"],
    maskShow: false,
    cinemaArr: [], // 影院列表
    cinemaIndex: "",
    currentIndex: "",
    cinemaName: "",
    cinemaId: "",
    cityName: "",  // 城市
    orderNo: "",
  },

  // 选择日期
  bindDateChange(e){
    this.setData({
      orderDate: e.detail.value
    })
  },

  // 影院
  cinematap(){
    this.setData({
      maskShow: true
    })
  },

  // 选择区域
  areaChoose: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      cinemaArr: this.data.areaList[index].children,
      currentIndex: index
    })
  },

  // 选择影院
  chooseCinema: function (e) {
    this.setData({
      cinemaIndex: e.currentTarget.dataset.index,
    });
  },

  // 确定选择影院
  sureCinema: function () {
    this.setData({
      cinemaName: this.data.cinemaArr[this.data.cinemaIndex].cinema_name,
      cinemaId: this.data.cinemaArr[this.data.cinemaIndex].region_id,
      maskShow: false
    });
  },

  // 取消
  hideMask: function () {
    this.setData({
      maskShow: false
    });
  },

  // 是否同意协议
  checkboxChange(e) {
    this.setData({
      ischecked: !this.data.ischecked
    });
  },

  // 提交订单
  submitOrder(e){
    if (this.data.ischecked == false){
      app.showError("请阅读并同意投放协议");
    }else if(this.data.imgUrl == ""){
      app.showError("请上传图片");
    }else{
      this.submitOrderMsg();
    }
  },

  // 编辑图片
  goEditImg(){
    wx.navigateTo({
      url: '../editImg/editImg?editType="collect"',
    })
  },

  // 提交订单信息
  submitOrderMsg(){
    wx.showLoading({
      title: '提交中',
    })
    wx.request({
      url: app.globalData.baseUrl + '/spLike/spLikeSuccess',
      header:{
        unicorn: app.globalData.token
      },
      data:{
        img: this.data.imgUrl,
        cinemaName: this.data.cinemaName,
        orderNo: this.data.orderNo,
        playTime: this.data.orderDate
      },
      success: res =>{
        console.log(res);
        if(res.data){
          if (res.data.errCode == "00000") {
            wx.showToast({
              title: '提交成功',
            })
          } else if (res.data.errCode == "00002") {
            app.showError("参数错误")
          } else if (res.data.errCode == "00006") {
            app.showError("条件未达成")
          } else if (res.data.errCode == "00007") {
            app.showError("该上屏次数已经使用过")
          }
          wx.hideLoading();
        }
      },
      fail: res =>{
        console.log(res);
        app.showError("服务器错误")
      }
    })
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var date = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
    this.setData({
      orderDate: app.getCurrentDate(1, date)
    });
    
    if(e.orderNo){
      this.setData({
        orderNo: e.orderNo
      })
    }

    if (app.globalData.chooseCity != "") {
      this.setData({
        cityName: app.globalData.chooseCity
      })
    } else {
      this.setData({
        cityName: app.globalData.locationCity
      })
    }
    this.getCinema();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.resultImgUrl != "") {
      this.setData({
        imgUrl: app.globalData.resultImgUrl
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 获取影院
  getCinema: function () {
    wx.showLoading({
      title: '加载中',
    });
    var data = {};
    if (app.globalData.chooseCity == "" || app.globalData.chooseCity == app.globalData.locationCity) {
      data = {
        'access_token': app.globalData.accessToken,
        'city_name': this.data.cityName,
        'current_lon': app.globalData.longitude,
        'current_lat': app.globalData.latitude,
      }
    } else {
      data = {
        'access_token': app.globalData.accessToken,
        'city_name': this.data.cityName,
      }
    }
    wx.request({
      url: app.globalData.url + '/ad/cinema/list',
      method: "POST",
      data: data,
      success: res => {
        var areaList = res.data.data.cinema_list.all_cinema_info;
        var locationCinema = res.data.data.cinema_list.current_cinema_info;
        this.setData({
          areaList: areaList,
          cinemaName: locationCinema.cinema_name,
          cinemaId: locationCinema.region_id,
          cinemaArr: areaList[0].children
        });
        wx.hideLoading();
      },
      fail: res => {
        console.log(res)
      }
    })
  },

  // 查看协议
  showProtocol: function () {
    this.setData({
      protocolMaskShow: true
    })
  },

  // 协议隐藏
  proTocolSure: function () {
    this.setData({
      protocolMaskShow: false
    })
  }
})